package com.dxl.service.sysprop;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dxl.common.model.Order;
import com.dxl.common.spring.AppCtxHolder;
import com.dxl.common.utils.Utils;
import com.dxl.entity.sysprop.SysProp;
import com.dxl.service.common.BaseService;

public class SysPropCacheManager {
	private static Map<String, List<SysProp>> index;
	private static String ROOT_DIR;
	
	public static void init() {
		BaseService service = AppCtxHolder.getBean("baseService");
		List<SysProp> rows = service.list(SysProp.class, Order.asc("propKey"), Order.asc("orderValue"));
		Map<String, List<SysProp>> index = new LinkedHashMap<String, List<SysProp>>();
		List<SysProp> tmp;
		for (SysProp row : rows) {
			tmp = index.get(row.getPropKey());
			if (Utils.isNull(tmp)) {
				tmp = new ArrayList<SysProp>();
				index.put(row.getPropKey(), tmp);
			}
			tmp.add(row);
		}
		SysPropCacheManager.index = index;
	}
	
	public static SysProp getProp(String propKey) {
		List<SysProp> props = index.get(propKey);
		return Utils.isNotEmpty(props) ? props.get(0) : null;
	}
	
	public static String getPropValue(String propKey) {
		SysProp prop = getProp(propKey);
		return Utils.isNull(prop) ? null : prop.getPropValue();
	}
	
	public static List<SysProp> getProps(String propKey) {
		return index.get(propKey);
	}
	
	public static void setRootDir(String dir) {
		ROOT_DIR = dir;
	}
	
	public static String getRootDir() {
		return ROOT_DIR;
	}
}
